package test1.main;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;


import java.io.IOException;
import java.util.ArrayList;

/**
 * ClassName: TestHBase
 * Package: test1.fun
 * Description:
 *         批量Put;
 * 	       批量Get;
 * 	       Scan指定范围扫描;
 * 	       批量Delete;
 * @Author ChenJun
 * @Create 2023/4/25 8:38
 * @Version 1.0
 */
public class TestHBase {

    //TODO 静态代码块创建连接
    private static Connection connection;

    static {
        try {
            Configuration configuration = HBaseConfiguration.create();
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //TODO  插入数据
    public static void putData(String tableName, String rowKey, String cf, String column, String value) throws IOException {
        //获取表名
        Table table = connection.getTable(TableName.valueOf(tableName));

        Put put = new Put(rowKey.getBytes());
        put.addColumn(cf.getBytes(), column.getBytes(), value.getBytes());

        Put put1 = new Put(rowKey.getBytes());
        put1.addColumn("1002".getBytes(), column.getBytes(), value.getBytes());

        //创建put集合
        ArrayList<Put> puts = new ArrayList<>();
        puts.add(put);
        puts.add(put1);

        table.put(puts);

        table.close();
    }

    //TODO Scan指定范围扫描
    public static void scanData(String tableName, String start, String stop) throws IOException {
        //获取表格
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建scan对象
        Scan scan = new Scan();
        scan.withStartRow(start.getBytes());
        scan.withStopRow(stop.getBytes());

        //创建过滤器
        ValueFilter valueFilter = new ValueFilter(
                CompareOperator.EQUAL,
                new SubstringComparator("chen")
        );


        //过滤器集合
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(valueFilter);
        filterList.addFilter(valueFilter);

        //添加过滤器
        scan.setFilter(filterList);

        //执行扫描
        ResultScanner results = table.getScanner(scan);

        //解析ResultScanner
        for (Result result : results) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RK:" + new String(CellUtil.cloneRow(cell)) +
                        ",CF:" + new String(CellUtil.cloneFamily(cell)) +
                        ",CN:" + new String(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + new String(CellUtil.cloneValue(cell)));
            }
        }
    }

    //TODO  批量get
    public static  void getData(String tableName, String rowKey, String cf, String column) throws IOException {
        //获取表格
        Table table = connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(rowKey.getBytes());
        Get get1 = new Get(rowKey.getBytes());

        ArrayList<Get> gets = new ArrayList<>();
        gets.add(get);
        gets.add(get1);

        table.get(gets);
        table.close();
    }

    //TODO 批量删除
    public static void deleteDate(String tableName, String rowKey, String cf, String column) throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Delete delete = new Delete(rowKey.getBytes());
        Delete delete1 = new Delete("1004".getBytes());

        //指定到列
//        delete.addFamily(cf.getBytes());

        delete.addColumn(cf.getBytes(),column.getBytes()); //删除指定列的最大版本(单个版本)
        delete1.addColumns(cf.getBytes(),column.getBytes());//删除指定列的所有版本(多个版本)

        //创建集合
        ArrayList<Delete> deletes = new ArrayList<>();
        deletes.add(delete);
        deletes.add(delete1);

        //执行操作
        table.delete(deletes);

        //关闭资源
        table.close();
    }

    public static void main(String[] args) throws IOException {

      //插入数据
      putData("stu1","1001","info","name","chenjun");

      //批量获取数据
      getData("stu1","1001","info","name");

      //按照范围扫描
      scanData("stu1","1001","1005");

      //批量删除
      deleteDate("stu1","1001","info","name");
      connection.close();
    }
}
